﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace FirstClassAssignment {
    public partial class QAndA : Form {

        private int[] _nums;
        private int _answer;
        
        private static int[] QuestionArray() {
            var nums = new int[3];
            var rand = new Random();
            nums[0] = rand.Next(21);
            nums[1] = rand.Next(2);
            nums[2] = rand.Next(21);

            return nums;
        }
        
        private void ShowScore() {
            score.SetBounds(280, 100, 50, 50);
            confirm.Visible = false;
            input.Visible = false;
            question.Visible = false;
            thisTime.Visible = false;
            allTime.Visible = false;
            count.Visible = false;
            label2.Visible = false;
            timer.Stop();
        }
        private void Timer_Tick(object sender, EventArgs e) {
            var at = int.Parse(allTime.Text);
            var tt = int.Parse(thisTime.Text);
            allTime.Text  = (at - 1).ToString();
            thisTime.Text = (Math.Min(at,tt) - 1).ToString();
            if (tt == 1) {
                ShowQuestion();
            }
            if (at == 1) {
                ShowScore();
            }
        }

        private void ShowQuestion() {
            thisTime.Text = "10";
            _nums = QuestionArray();
            if (_nums[1] == 0) {
                question.Text = _nums[0] + " + " + _nums[2];
                _answer = _nums[0] + _nums[2];
            }
            else {
                question.Text = _nums[0] + " - " + _nums[2];
                _answer = _nums[0] - _nums[2];
            }
            input.Text = "";
        }


        private void confirm_Click(object sender, System.EventArgs e) {
            var userAnswer = int.Parse(input.Lines[input.Lines.Length - 1].Trim());
            var sco = int.Parse(score.Text);
            if (userAnswer == _answer) {
                input.BackColor = Color.Green;
                score.Text =(sco + 10).ToString();
            }
            else {
                input.BackColor = Color.Red;
            }
            
            count.Text = (int.Parse(count.Text) + 1).ToString();

            if (int.Parse(count.Text) == 21)
                ShowScore();
            ShowQuestion();
        }

        private void Execute() {
            confirm.Click += confirm_Click;
            timer.Tick += Timer_Tick; 
            timer.Start();
            ShowQuestion();
        }
        public QAndA() {
            InitializeComponent();
            Execute();
        }
    }
}
